package org.hfy.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.hfy.mapper.InboundMapper;

/**
 * 厂内编号生成器
 * 
 * @author hfy
 * @version 1.0
 */
@Component
public class FactoryNoGenerator {

    @Autowired
    private InboundMapper inboundMapper;

    /**
     * 生成厂内编号
     * 格式：DQ + 6位序号，从 DQ624865 开始递增
     * 
     * @return 厂内编号
     */
    public String generateFactoryNo() {
        // 查询当前最大编号
        String maxNo = inboundMapper.selectMaxFactoryNo();
        int nextNo = 624865; // 起始编号
        
        if (maxNo != null && maxNo.startsWith("DQ")) {
            try {
                int currentNo = Integer.parseInt(maxNo.substring(2));
                nextNo = currentNo + 1;
            } catch (NumberFormatException e) {
                nextNo = 624865; // 如果解析失败，使用起始编号
            }
        }
        
        return String.format("DQ%06d", nextNo);
    }

    /**
     * 验证厂内编号格式
     * 
     * @param factoryNo 厂内编号
     * @return 是否合法
     */
    public boolean isValidFactoryNo(String factoryNo) {
        if (factoryNo == null || factoryNo.length() != 8) {
            return false;
        }
        
        if (!factoryNo.startsWith("DQ")) {
            return false;
        }
        
        try {
            Integer.parseInt(factoryNo.substring(2));
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    }
} 